# 专门处理文件上传存储的
import qiniu


# 需要填写你的 Access Key 和 Secret Key
access_key = 'yV4GmNBLOgQK-1Sn3o4jktGLFdFSrlywR2C-hvsW'
secret_key = 'bixMURPL6tHjrb8QKVg2tm7n9k8C7vaOeQ4MEoeW'
# 要上传的空间
bucket_name = 'ihome'



def upload_file(data):
    '''
    上传文件到七牛云
    :param data: 要上传的文件的二进制
     
    '''

    # 构建鉴权对象
    q = qiniu.Auth(access_key, secret_key)

    # 生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name)

    # 上传文件
    ret, info = qiniu.put_data(token, None, data)

    # print(ret['key'])

    if info.status_code != 200:
        raise Exception("上传文件到七牛失败")


    return ret['key']






# if __name__ == '__main__':
#     path = 'E:\suolong.jpg'
#     with open(path, "rb") as file:
#         upload_file(file.read())